package com.framework
{
	import com.framework.util.Log;
	import com.greensock.TweenLite;
	
	import flash.display.MovieClip;
	import flash.display.Stage;

	public class GG
	{
		public static var stage:Stage;
		public static var elapsedTime:Number;
		public static var lastTime:Number;
		
		/**
		 * Ratio of pixels to meters 
		 */		
		public static const RATIO:Number = 30;
		
		public static var STAGE_WIDTH:Number;
		public static var STAGE_HEIGHT:Number;
		
		
		
		//Gère la transition des states
		public static var currentState:BasicState;
		public static var newState:BasicState;
		public static var isSwitching:Boolean = false;
		public static var panneauHaut:MovieClip = new PANNEAU_TRANSITION();
		public static var panneauBas:MovieClip = new PANNEAU_TRANSITION();
		
		public static function switchState(state:BasicState):void
		{
			//Indique qu'on switch
			isSwitching = true;
			newState = state;
			
			//Démarre la transition des panneaux
			startPanneauTransition();

		}
		private static function startPanneauTransition():void
		{
			panneauHaut.y = -panneauHaut.height;
			panneauBas.y = STAGE_HEIGHT;
			stage.addChild(panneauHaut);
			stage.addChild(panneauBas);
			
			TweenLite.to(panneauHaut,0.5,{y:0});
			TweenLite.to(panneauBas,0.5,{y:STAGE_HEIGHT/2, onComplete:removeCurrentState});
		}
		private static function removeCurrentState():void
		{
			//Supprime la state actuelle
			currentState.destroy();
			GG.stage.removeChild(currentState);
			
			//Ajoute la nouvelle state
			currentState = newState;
			stage.addChild(newState);
			
			//Replace les panneaux au dessus
			stage.setChildIndex(panneauBas,stage.numChildren-1);
			stage.setChildIndex(panneauHaut,stage.numChildren-1);
			
			//Créé la state
			newState.create();
			
			//Termine la transition
			endPanneauTransition();
			
			
		}
		private static function endPanneauTransition():void
		{
			TweenLite.to(panneauHaut,0.5,{y:-panneauHaut.height});
			TweenLite.to(panneauBas,0.5,{y:STAGE_HEIGHT, onComplete:removePanneauTransition});	
		}
		private static function removePanneauTransition():void
		{
			stage.removeChild(panneauBas);
			stage.removeChild(panneauHaut);
			
			//Indique la fin du switch
			isSwitching = false;
		}
		
	}
}